<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">仓位编号</label>
        <el-input v-model="query.smartAddr" clearable placeholder="请输入仓位编号搜索" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">使用状态</label>
        <el-select
          v-model="query.status"
          clearable
          size="small"
          placeholder="是否使用"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in statusTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <label class="el-form-item-label">订单标识</label>
        <el-input v-model="query.uuid" clearable placeholder="请输入订单标识搜索" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">开始时间</label>
        <date-range-picker
          v-model="query.startTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />
        <label class="el-form-item-label">结束时间</label>
        <date-range-picker
          v-model="query.endTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="95px">
          <el-form-item label="使用用户" prop="createBy">
            <el-input v-model="form.createBy" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="充电桩名称" prop="cabinetName">
            <el-input v-model="form.cabinetName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="充电桩位置" prop="cabinetLocation">
            <el-input v-model="form.cabinetLocation" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="仓位编号" prop="smartAddr">
            <el-input v-model="form.smartAddr" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="使用状态">
            <el-radio-group v-model="form.status" disabled  style="width: 178px">
              <el-radio :label="true">使用中</el-radio>
              <el-radio :label="false">已完成</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="订单标识" prop="uuid">
            <el-input v-model="form.uuid" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="开始时间">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结束时间">
            <el-date-picker v-model="form.endTime" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data"
        size="small" style="width: 100%;margin-top: 1%;"
      @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column prop="createBy" label="使用用户" align="center"/>
        <el-table-column prop="cabinetName" label="充电桩名称" align="center"/>
        <el-table-column prop="cabinetLocation" label="充电桩位置" align="center"/>
        <el-table-column prop="smartAddr" label="仓位编号" align="center"/>
        <el-table-column prop="status" label="当前使用状态" align="center">
          <template slot-scope="scope">
            {{scope.row.status}}
          </template>
        </el-table-column>
        <el-table-column prop="uuid" label="订单标识" align="center"/>
        <el-table-column label="使用时长" align="center">
          <template slot-scope="scope">
            {{ scope.row.endTime == undefined ? '使用中' : parseInt(((( new Date(scope.row.endTime) - new Date(scope.row.startTime) ).valueOf()) % (1000 * 60 * 60)) / (1000 * 60)) + 'minutes'}}
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" align="center"/>
        <el-table-column prop="endTime" label="结束时间" align="center"/>
        <el-table-column v-if="checkPer(['admin','cabinetLog:edit','cabinetLog:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCabinetLog from '@/api/order/cabinetLog.js'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, cabinetName: null, cabinetLocation: null, createBy: null, endTime: null, smartAddr: null, startTime: null, status: null, uuid: null, userId: null }
export default {
  name: 'CabinetLog',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '充电桩使用信息', url: 'api/cabinetLog', idField: 'id', sort: 'id,desc', crudMethod: { ...crudCabinetLog }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'cabinetLog:add'],
        edit: ['admin', 'cabinetLog:edit'],
        del: ['admin', 'cabinetLog:del']
      },
      rules: {
        cabinetName: [
          { required: true, message: '充电桩名称不能为空', trigger: 'blur' }
        ],
        cabinetLocation: [
          { required: true, message: '充电桩位置不能为空', trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: '创建者不能为空', trigger: 'blur' }
        ],
        smartAddr: [
          { required: true, message: '仓位编号不能为空', trigger: 'blur' }
        ],
        uuid: [
          { required: true, message: '订单标识不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'smartAddr', display_name: '仓位编号' },
        { key: 'status', display_name: '当前使用状态' },
        { key: 'uuid', display_name: '订单标识' }
      ],
      statusTypeOptions: [
        { key: 'true', display_name: '使用中' },
        { key: 'false', display_name: '已完成' }
      ],
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
